package game.module.item.processor;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.item.logic.ItemManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageHall.C2SUseItem;
import lua.LuaMessageHall.S2CUseItem;
import lua.LuaMessageLogin.S2ErrorCode;

@MsgCodeAnn(msgcode = C2SUseItem.id, accessLimit = 500)
public class UseItemProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(UseItemProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		C2SUseItem reqUseItem = C2SUseItem.parse(requestMessage);
		int playerId = playingRole.getId();
		logger.info("player use items,playerId={},template item id={},cnt={}", playerId, reqUseItem.item_id, reqUseItem.cnt);
		
		int resp = ItemManager.getInstance().useItem(playingRole, reqUseItem.item_id, reqUseItem.cnt);
		if (resp != 1) {
			S2ErrorCode retMsg = new S2ErrorCode(resp);
			playingRole.writeAndFlush(retMsg.build(playingRole.alloc()));
			return;
		}
		S2CUseItem respMsg = new S2CUseItem();
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
